package com.leilei.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.leilei.entity.Knife4jProperties;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lei
 * @create 2022-05-16 15:16
 * @desc 自动装配配置类
 **/
@Log4j2
@EnableKnife4j
@Configuration
@EnableSwagger2WebMvc
@ConditionalOnWebApplication
@EnableConfigurationProperties(Knife4jProperties.class)
@ConditionalOnProperty(prefix = Knife4jProperties.PREFIX, name = "enable", havingValue = "true", matchIfMissing = true)
public class Knife4jAutoConfiguration {
    private final Knife4jProperties properties;

    public Knife4jAutoConfiguration(Knife4jProperties knife4jProperties) {
        this.properties = knife4jProperties;
    }

    private static final String SUPER_ADMIN_TOKEN = "aaa.bbb.ccc";

    @Bean
    public Docket apiConfig() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .enable(properties.isEnable())
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(globalOperationParameters());
        log.info("内置文档信息加载成功!");
        return docket;
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(properties.getTitle())
                .description(properties.getDesc())
                .contact(new Contact(properties.getAuthor(), properties.getUrl(), properties.getEmail()))
                .termsOfServiceUrl("")
                .version(properties.getVersion())
                .build();
    }

    public List<Parameter> globalOperationParameters() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name(properties.getHeader())
                .description("JWT-TOKEN")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue(SUPER_ADMIN_TOKEN)
                .required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }


}
